package com.mall.web.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.protocol.HTTP;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.pay.config.PayConfig;
import com.mall.pay.wxpay.config.WxpayConfig;
import com.mall.pay.wxpay.util.IpUtils;
import com.mall.pay.wxpay.util.WxApi;
import com.mall.untils.cache.CacheUtil;
import com.mall.untils.core.GenerateQrCodeUtil;
import com.mall.web.service.WxService;
import net.sf.json.JSONObject;

/**
 * @author lisj
 * @time 2016年7月20日 下午2:03:55
 */
@Controller
@RequestMapping(value = "/wxpay")
public class WxController {

	private @Resource WxService wxService;

	/**
	 * 扫码支付接口 说明：一步直接生成微信二维码，不需要传入url,需要传入商品信息
	 */
	@RequestMapping(value = "createQrcode")
	@ResponseBody
	public void getQRCode(HttpServletRequest request, HttpServletResponse response) {
		JSONObject product = JSONObject.fromObject(request.getParameter("data"));
		try {
			product.put("ip", IpUtils.getIpAddr(request));
			String code_url = wxService.getCodeurl(product);
			GenerateQrCodeUtil.encodeQrcode(code_url, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * H5支付调用接口
	 * 
	 * @return json 微信统一下单返回的package
	 */
	@RequestMapping(value = "getPackage")
	@ResponseBody
	public String getPackage(HttpServletRequest request) {
		Map<String, Object> product = WebUtils.getParametersStartingWith(request, "");
		try {
			product.put("ip", IpUtils.getIpAddr(request));
			return wxService.getPackage(product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 微信支付回调函数
	 */
	@RequestMapping(value = "wxNotify")
	@ResponseBody
	public void wxNotify(HttpServletRequest request, HttpServletResponse response) {
		try {
			wxService.wxNotify(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取生成微信二维码需要的微信url
	 * 
	 * @return String 微信url
	 */
	@RequestMapping(value = "getCodeurl")
	@ResponseBody
	public String getCodeurl(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> product = WebUtils.getParametersStartingWith(request, "");
		try {
			product.put("ip", IpUtils.getIpAddr(request));
			return wxService.getCodeurl(product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 生成微信二维码
	 * 
	 * @param code_url
	 *            生成二维码需要的url
	 */
	@RequestMapping(value = "getQrcode")
	@ResponseBody
	public void getQRCode(String code_url, HttpServletResponse response) {
		try {
			GenerateQrCodeUtil.encodeQrcode(code_url, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询微信订单支付是否成功
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "orderIsSuccess")
	@ResponseBody
	public String orderIsSuccess(HttpServletRequest request) {
		Map<String, Object> paramMap = WebUtils.getParametersStartingWith(request, "");
		return wxService.orderIsSuccess(paramMap);
	}

	/**
	 * 支付缓存的清除
	 * 
	 * @return
	 */
	@RequestMapping(value = "/clearPayCache")
	@ResponseBody
	public String clearPayCache() {
		return CacheUtil.getInstance().setCache(PayConfig.payConfigKey, null, 1);
	}

	/**
	 * 测试平台回调的地址
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "testNotify")
	@ResponseBody
	public void testNotify(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println("----testNotify---");
			// 读取请求内容
			BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			// 将资料解码
			String reqBody = sb.toString();
			System.out.println(URLDecoder.decode(reqBody, HTTP.UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
